package hn.sigit.view.workflow.transaction;

import hn.sigit.logic.geometry.GeometryOperations;
import hn.sigit.logic.geometry.MergeException;
import hn.sigit.logic.ladm.RightsUtil;
import hn.sigit.model.hnd.ladmshadow.Parcel;
import hn.sigit.model.hnd.ladmshadow.Property;
import hn.sigit.dao.hnd.ladmshadow.ParcelDAO;
import hn.sigit.dao.hnd.ladmshadow.PropertyDAO;
import hn.sigit.util.NoEqualRightsException;

import java.io.Serializable;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import com.vividsolutions.jts.geom.Polygon;

public class DataEntryMergeHelper implements Serializable {
	private static final long serialVersionUID = 1L;

	private DataEntryHelper dataEntryHelper;
	
	private Long parcelToMergeId1;
	private Long parcelToMergeId2;
	
	public DataEntryMergeHelper(DataEntryHelper dataEntryHelper) {
		this.dataEntryHelper = dataEntryHelper;
	}
	
	public void doMerge() throws MergeException, NoEqualRightsException {
		Parcel parcel1 = ParcelDAO.loadParcelById(parcelToMergeId1);
		Property property1 = parcel1.getProperty();
		
		Parcel parcel2 = ParcelDAO.loadParcelById(parcelToMergeId2);
		Property property2 = parcel2.getProperty();
		
		//TODO: comparar que los RRRs sean compatibles
		if (RightsUtil.propertyRightsEqual(property1, property2)) {
			Polygon shape1 = parcel1.getShape();
			Polygon shape2 = parcel2.getShape();
			
			Date timeStamp = new Date();

			Polygon newShape = GeometryOperations.mergePolygons(shape1, shape2);

			Parcel newParcel = parcel1.clone();
			newParcel.setBeginLifespanVersion(timeStamp);
			newParcel.setEndLifespanVersion(null);
			newParcel.setShape(newShape);
			newParcel.setFieldTab(dataEntryHelper.getFieldTabCounter());
			newParcel.setModified(false);
			newParcel.setOriginal(false);
			
			Property newProperty = property1.clone();
			newProperty.setBeginLifespanVersion(timeStamp);
			newProperty.setEndLifespanVersion(null);
			newProperty.getSpatialUnits().clear();
			newProperty.getSpatialUnits().add(newParcel);
			newProperty.setModified(false);
			newProperty.setOriginal(false);

			newParcel.getBaunits().clear();
			newParcel.getBaunits().add(newProperty);

			parcel1.setModified(true);
			parcel1.setEndLifespanVersion(timeStamp);
			property1.setEndLifespanVersion(timeStamp);
			
			parcel2.setModified(true);
			parcel2.setEndLifespanVersion(timeStamp);
			property2.setEndLifespanVersion(timeStamp);

			ParcelDAO.save(parcel1);
			ParcelDAO.save(parcel2);
			ParcelDAO.save(newParcel);
			PropertyDAO.save(property1);
			PropertyDAO.save(property2);
			PropertyDAO.save(newProperty);
			
			dataEntryHelper.setSelectedParcel(newParcel);
		}
		else
			throw new NoEqualRightsException("dataentry.merge.error_noequal_rights");
	}

	public String applyMerge() {
		try {
			doMerge();
		}
		catch (MergeException me) {
			FacesContext.getCurrentInstance().addMessage("",
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							dataEntryHelper.getResBundle().loadMessage(me.getExceptionBundle()) + ": ",
							dataEntryHelper.getResBundle().loadMessage(me.getMessage())));
		}
		catch (NoEqualRightsException nere) {
			FacesContext.getCurrentInstance().addMessage("",
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							dataEntryHelper.getResBundle().loadMessage(nere.getExceptionBundle()) + ": ",
							dataEntryHelper.getResBundle().loadMessage(nere.getMessage())));
		}
			
		return null;
	}
	
	public Long getParcelToMergeId1() {
		return parcelToMergeId1;
	}
	public void setParcelToMergeId1(Long parcelToMergeId1) {
		this.parcelToMergeId1 = parcelToMergeId1;
	}

	public Long getParcelToMergeId2() {
		return parcelToMergeId2;
	}
	public void setParcelToMergeId2(Long parcelToMergeId2) {
		this.parcelToMergeId2 = parcelToMergeId2;
	}


}
